//
//  PublicRequestTools.m
//  AiShangJinFuSalesman
//
//  Created by AiShangTong on 2017/6/27.
//  Copyright © 2018年 AiShangTong. All rights reserved.
//

#import "PublicRequestTools.h"


NSString *const PublicRequestItunesAboutAppleURL = @"https://itunes.apple.com/lookup";  //用户更新检查

@implementation PublicRequestTools


+(void)itunesUpdateChreak {
    
    UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"更新" message:@"版本已更新，为了审核的更加顺利请前往更新!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id1423507466"];
        [[UIApplication sharedApplication]openURL:url];
        
    }];
    
    [alertVC addAction:action];
    [[HFQCommonTools lastWindow].rootViewController presentViewController:alertVC animated:YES completion:nil];
}

/**
 检查iTunes版本

 */
+ (void)cheakAppItunesUpdate {

    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"1423507466" forKey:@"id"];
    
    [[RequestManager manager] HFQDataCommonRequest_Mao:RequestMethodPost URLString:PublicRequestItunesAboutAppleURL parameters:dic success:^(id  _Nullable responseObject) {
        NSDictionary * dataDict = responseObject;
        if ([dataDict[@"resultCount"] boolValue]) {
            
            NSArray *infoArray = [dataDict objectForKey:@"results"];
            if ([infoArray count]) {
                
                NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
                NSString *lastVersion = [releaseInfo objectForKey:@"version"];
                NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
                NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
                lastVersion=[lastVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
                currentVersion=[currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
                NSLog(@"lastVersion:%@-----currentVersion:%@",lastVersion,currentVersion);
                
                if (([lastVersion intValue]>[currentVersion intValue])) {
                    
                    [PublicRequestTools itunesUpdateChreak];
                }
            }
            
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
    }];
}

@end
